﻿using FastReport.Export.PdfSimple;
using FastReport.Web;
using QMERP.Infrastructure.Tools;

namespace QMERP.Infrastructure.Extensions
{
    public static class FastReportExtension
    {
        /// <summary>
        /// 列表转Pdf报表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static MemoryStream ToPdfReport<T>(this List<T> list)
        {
            Type type = typeof(T);
            string fileName = type.Name;
            var filePath = $"{AppContext.BaseDirectory + ConfigurationUtils.GetSection("PdfTemplatePathName") + fileName}.frx";
            MemoryStream stream = new();
            WebReport report = new();
            report.Report.Load(filePath);
            report.Report.RegisterData(list, ConfigurationUtils.GetSection("PdfReportAlias"));
            report.Report.Prepare();
            PDFSimpleExport export = new();
            report.Report.Export(export, stream);
            return stream;
        }
    }
}
